import { ActionContext } from 'vuex';
import cloneDeep from 'lodash/cloneDeep';
import { FormCard } from '@/model/types/formCard';
import { FormProperty } from '@/model/designer/controlProperty';
import { ResponseHandle } from '@/global/util/preUtils';
import {
  getGroupList,
  createGroup,
  renameGroup,
  removeGroup,
  sortGroup,
  getFormList,
  createForm,
  renameForm,
  removeForm,
  sortForm,
  createFormBiz,
  getFormBiz,
  removeFormBiz,
  saveFormBiz,
} from '@/views/formHome/api';
import { getFormData } from '@/core/api';

interface State {
  mode: string;
  curSchemaCode: string;
  curSchemaName: string;
  isLoading: boolean;
  form: {
    formCards: FormCard[];
    formProperty: { [key: string]: any };
  };
  dataSource: { [key: string]: any }[];
  selectedRowKeys: string[];
}

const getInitData = (): State => {
  return {
    mode: 'read',
    curSchemaCode: '',
    curSchemaName: '',
    isLoading: false,
    form: {
      formCards: [],
      formProperty: {},
    },
    dataSource: [],
    selectedRowKeys: [],
  };
};
const state: State = getInitData();
const getters = {};
const actions = {
  async getFormData(context: ActionContext<State, any>, schemaCode: string) {
    context.commit('SetIsLoading', true);
    const res: any = await getFormData({ schemaCode });
    context.commit('SetIsLoading', false);
    ResponseHandle(res, () => {
      res.data && context.commit('SetForm', res.data);
    });
  },
  async getGroupList(context: ActionContext<State, any>, params: any) {
    const res: any = await getGroupList(params);
    return res;
  },
  async createGroup(context: ActionContext<State, any>, params: any) {
    const res: any = await createGroup(params);
    return res;
  },
  async renameGroup(context: ActionContext<State, any>, params: any) {
    const res: any = await renameGroup(params);
    return res;
  },
  async removeGroup(context: ActionContext<State, any>, formGroup: string) {
    const res: any = await removeGroup({ formGroup });
    return res;
  },
  async sortGroup(context: ActionContext<State, any>, params: any) {
    const res: any = await sortGroup(params);
    return res;
  },
  // 表单
  async getFormList(context: ActionContext<State, any>, params: any) {
    const res: any = await getFormList(params);
    return res;
  },
  async createForm(context: ActionContext<State, any>, params: any) {
    const formProperty = cloneDeep(FormProperty);
    const { schemaCode, schemaName, formGroup, pos } = params;
    formProperty.schemaCode.defaultValue = schemaCode;
    formProperty.schemaName.defaultValue = schemaName;
    const res: any = await createForm(
      Object.assign(
        {},
        { schemaCode, schemaName, formGroup, pos, formProperty, formCards: [] },
      ),
    );
    return res;
  },
  async renameForm(context: ActionContext<State, any>, params: any) {
    const res: any = await renameForm(params);
    return res;
  },
  async removeForm(context: ActionContext<State, any>, schemaCode: string) {
    const res: any = await removeForm({ schemaCode });
    return res;
  },
  async sortForm(context: ActionContext<State, any>, params: any) {
    const res: any = await sortForm(params);
    return res;
  },
  async createFormBiz(context: ActionContext<State, any>, params: any) {
    const res: any = await createFormBiz(params);
    return res;
  },
  async getFormBiz(context: ActionContext<State, any>, params: any) {
    const res: any = await getFormBiz(params);
    context.commit('SetDataSource', res.data ?? []);
    return res;
  },
  async removeFormBiz(context: ActionContext<State, any>, params: any) {
    const res: any = await removeFormBiz({
      bizObjectIds: context.state.selectedRowKeys,
    });
    return res;
  },
  async saveFormBiz(context: ActionContext<State, any>, params: any) {
    const res: any = await saveFormBiz(params);
    return res;
  },
};
const mutations = {
  SetIsLoading(state: State, data: boolean) {
    state.isLoading = data;
  },
  SetMode(state: State, data: string) {
    state.mode = data;
  },
  SetForm(state: State, data: any) {
    state.form = data;
  },
  ClearFormCardsValue(state: State) {
    state.form.formCards = state.form.formCards.map((it) => {
      if (it.controls?.ctlValue) {
        it.controls.ctlValue.value = null;
      }
      if (it.columns?.length) {
        it.columns = it.columns.map((el) => {
          if (el.controls?.ctlValue) {
            el.controls.ctlValue.value = null;
          }
          return el;
        });
      }
      return it;
    });
  },
  SetCurSchemaCode(state: State, id: string) {
    state.curSchemaCode = id;
  },
  SetCurSchemaName(state: State, id: string) {
    state.curSchemaName = id;
  },
  SetDataSource(state: State, data: []) {
    state.dataSource = data;
  },
  SetSelectedRowKeys(state: State, data: []) {
    state.selectedRowKeys = data;
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
